<?php
namespace Home\Biz;

class OrderBiz{
	
	public static function search($param_en){
		
		// 参数检查start
		if(empty($param_en['page_index'])){
			$param_en['page_index'] = 1;
		}
		
		if(empty($param_en['page_size'])){
			$param_en['page_size'] = C('OrderConfig.listpage_search_default_size');
		}
		// 参数检查end
		
		
		// 搜索条件start
 		$condition_array = array();
		
		if(!empty($param_en['promoter_id'])){
			$condition_array['promoter_id'] = array('like', "%".$param_en['promoter_id']."%");
		}
		
		if(!empty($param_en['promoter_name'])){
			$condition_array['promoter_name'] = array('like', "%".$param_en['promoter_name']."%");
		}
		
		if(!empty($param_en['order_id'])){
			$condition_array['order_id'] = array('like', "%".$param_en['order_id']."%");
		}
		
		if(!empty($param_en['order_status'])){
			$condition_array['order_status'] = array('eq', $param_en['order_status']);
		}
		
		if(!empty($param_en['payment_status'])){
			$condition_array['payment_status'] = array('eq', $param_en['payment_status']);
		}

        if(!empty($param_en['order_type'])){
            $condition_array['order_type'] = array('eq', $param_en['order_type']);
        }
		
		if(!empty($param_en['begin_order_date'])){
			$condition_array['order_date'] = array('egt', $param_en['begin_order_date']);
		}
		
		if(!empty($param_en['end_order_date'])){
			$condition_array['order_date'] = array('elt', $param_en['end_order_date']);
		}
		// 搜索条件end
		
		
		
		$return_en = array();
		
		$total_count = M('promoterOrder')
		->where($condition_array)
		->count();
		$return_en['total_count'] = $total_count;
		
		
		$lst_order_en = M('promoterOrder')
		->page($param_en['page_index'], $param_en['page_size'])
		->order('create_date DESC')
		->where($condition_array)
		->select();

		foreach($lst_order_en as $key => $en){
			$lst_order_en[$key]['order_status_text'] = StringBiz::getPromoterOrderStatusText($en['order_status']);
			$lst_order_en[$key]['order_revenue_status_text'] = StringBiz::getPromoterOrderRevenueStatusText($en['order_revenue_status']);
			$lst_order_en[$key]['payment_status_text'] = StringBiz::getPaymentStatusText($en['payment_status']);
		}
		$return_en['lst_order_en'] = $lst_order_en;
	
		$total_page = 0;
		$rem = $total_count % $param_en['page_size'];
		if($rem == 0){
			$total_page = $total_count / $param_en['page_size'];
		}
		else{
			$total_page = floor($total_count / $param_en['page_size']) + 1;
		}

		$return_en['total_page'] = $total_page;
		
		return $return_en;
	}
	
	
	public static function cancelOrder($order_id, $user){
		//0. 数据校验
		if(empty($order_id)){
			return '不存在订单编号';
		}
		$order_en = M('orderInfo', '', 'DB_trade')
		->where(array('order_id'=>$order_id))
		->find();
		
		if(empty($order_en)){
			return '不存在的订单';
		}

	
		//1. 更新order_info
		M('orderInfo', '', 'DB_trade')->where(array('order_id'=>$order_id))->setField('order_status', 52);
		//2.写入order_status_change_log
		$order_status_change_log = array(
				'order_id'		=> $order_id,
				'order_status'	=> 52,
				'order_status_text'	=> '客服取消',
				'remark'		=> '',
				'create_user'	=> $user,
				'create_date'	=> date("y-m-d H:i:s", time()),
		);
		M('orderStatusChangeLog', '', 'DB_trade')->add($order_status_change_log);
	
	
		//3.写入order_cancel
		$order_cancel_en = array(
				'order_id'		=> $order_id,
				'role_type'		=> 1,
				'role_user_id'	=> $order_en['user_id'],
				'cancel_reason'	=> 1,
				'remark'		=> '',
				'create_user'	=> $user,
				'create_date'	=> date("y-m-d H:i:s", time()),
		);
		M('orderCancel', '', 'DB_trade')->add($order_cancel_en);


		//4.联动更新promoter_order表的订单状态
		M('promoterOrder')->where(array('order_id'=>$order_id))->setField('order_status', 52);
	}
	
	
	
	
	public static function stockOrderImport($file_path){
		Vendor("phpExcel.PHPExcel.IOFactory");
		if(!file_exists($file_path)){
			return 'file not find';
		}
		
		$objReader = \PHPExcel_IOFactory::createReader('Excel5');
		$PHPReader = $objReader->load($file_path);

		
		$PHPReader->setActiveSheetIndex(0); 
		$sheet		= $PHPReader->getActiveSheet();// 读取第一個工作表
		$highestRow = $sheet->getHighestRow(); // 取得总行数

		
		$lst_rrs_import_stock_order_en 	= array();
		$lst_rrs_stock_order_id			= array();
		for ($row = 1; $row <= $highestRow; $row++){//行数是以第1行开始
			$logistics_status_text	= $sheet->getCellByColumnAndRow(0, $row)->getValue();
			$rrs_stock_order_id		= $sheet->getCellByColumnAndRow(1, $row)->getValue();
			$logistics_no			= $sheet->getCellByColumnAndRow(2, $row)->getValue();
			//签收状态
			$logistics_sign_flag = 0;
			
			if($logistics_status_text == '签收成功'){
				$logistics_sign_flag = 1;
			}
			else{
				$logistics_sign_flag = 0;
			}
			
			
			$rrs_import_stock_order_en = array(
					'rrs_stock_order_id'	=> $rrs_stock_order_id,
					'logistics_no'			=> $logistics_no,
					'logistics_sign_flag'	=> $logistics_sign_flag
			);
			array_push($lst_rrs_import_stock_order_en, $rrs_import_stock_order_en);
			array_push($lst_rrs_stock_order_id, $rrs_stock_order_id);
		}

		self::ProccessImportStockOrder($lst_rrs_stock_order_id, $lst_rrs_import_stock_order_en);
	}
	
	/**
	 * 
	 * @param unknown $lst_rrs_stock_order_id 所有rrs出库单的编号集合
	 * @param unknown $lst_rrs_import_stock_order_en 所有rrs出库单的数据集合
	 */
	private static function ProccessImportStockOrder($lst_rrs_stock_order_id, $lst_rrs_import_stock_order_en){
		$lst_rrs_stock_order_id_condition_sql = "rrs_stock_order_id IN ('".implode("','", $lst_rrs_stock_order_id)."')";
		$lst_rrsStockOrder_en = M('rrsStockOrder', '', 'DB_trade')->where($lst_rrs_stock_order_id_condition_sql)->select();
		
		//本次导入所涉及到的订单编号集合
		$lst_import_order_id = array();
		
		
		//region 1.更改订单详情数据
		foreach($lst_rrs_import_stock_order_en as $rrs_import_stock_order_en){
			$rrs_stock_order_id = $rrs_import_stock_order_en['rrs_stock_order_id'];
			$logistics_no		= $rrs_import_stock_order_en['logistics_no'];
			$logistics_sign_flag= $rrs_import_stock_order_en['logistics_sign_flag'];
			
			$filter_lst_rrsStockOrder_en = array_filter($lst_rrsStockOrder_en, function($rrsStockOrder_item) use ($rrs_stock_order_id){
				return $rrsStockOrder_item['rrs_stock_order_id'] == $rrs_stock_order_id;
			});
			if(empty($filter_lst_rrsStockOrder_en)){
				continue;
			}

			$rrsStockOrder_en = current($filter_lst_rrsStockOrder_en);
			array_push($lst_import_order_id, $rrsStockOrder_en['order_id']);


            // 更新order_detail表的物流信息begin
			$logistics_status = 1;
			if(empty($logistics_no)){
				//物流处理中
				$logistics_status = 1;
			}
			else{
				if($logistics_sign_flag == 0){
					//已发货
					$logistics_status = 2;
				}
				else{
					//已签收
					$logistics_status = 3;
				}
			}
			
			
			$update_order_detail_en = array(
					'shipping_type'		=> 22,
					'logistics_no'		=> $logistics_no,
					'logistics_company'	=> '日日顺',
					'logistics_status'	=> $logistics_status,
			);
			
			M('orderDetail', '', 'DB_trade')->where(array('third_oid'=>$rrsStockOrder_en['third_oid']))->save($update_order_detail_en);
		}
		
		//endregion 1.更改订单详情数据
		
		
		//region 2.更改订单状态
		$lst_order_id_condition_sql = "order_id IN ('".implode("','", $lst_import_order_id)."')";
		$lst_order_detail_en = M('orderDetail', '', 'DB_trade')->where($lst_order_id_condition_sql)->select();
		
		foreach($lst_import_order_id as $order_id){
			$filter_lst_orderDetail_en1 = array_filter($lst_order_detail_en, function($order_detail_item) use($order_id){
				return ($order_detail_item['order_id'] == $order_id && 
						$order_detail_item['logistics_status'] == 1);
			});
			
			$order_status = 25;
			
			
			if(!empty($filter_lst_orderDetail_en1)){
				//只要存在一个物流状态为1（物流处理中），那订单状态就是25
				$order_status = 25;//物流处理中
			}
			else{
				$filter_lst_orderDetail_en2 = array_filter($lst_order_detail_en, function($order_detail_item) use($order_id){
					return ($order_detail_item['order_id'] == $order_id &&
							$order_detail_item['logistics_status'] == 2);
				});
				
				
				
				if(!empty($filter_lst_orderDetail_en2)){
					//不存在物流状态为1（物流处理中），只要存在任一物流状态为2（已发货），则订单状态就是31
					$order_status = 31;//已发货
				}
				else{
					$filter_lst_orderDetail_en3 = array_filter($lst_order_detail_en, function($order_detail_item) use($order_id){
						return ($order_detail_item['order_id'] == $order_id &&
								$order_detail_item['logistics_status'] == 3);
					});
					
					$filter_all_lst_orderDetail_en3 = array_filter($lst_order_detail_en, function($order_detail_item) use($order_id){
						return ($order_detail_item['order_id'] == $order_id);
					});
					
					if(count($filter_lst_orderDetail_en3) == count($filter_all_lst_orderDetail_en3)){
						//只有一张订单下的所有detail物流全部是3（已签收），此张订单状态才为35
						$order_status = 35;//已完成
					}
					else{
						$order_status = 26;//物流异常
					}
				}
			}

			//已完成的订单，不需要再次重复更新状态
            $order_en   = M('order_info', '', 'DB_trade')->where()->find();
            if($order_en['order_status'] == 35){
                continue;
            }

			//目前的订单状态，若与要更改的订单状态相同，则不需要重复更新
            if($order_en['order_status'] == $order_status){
                continue;
            }



			//region A.更改order_info的订单状态
			$update_orderInfo_en = array(
					'order_status' => $order_status
			);
			if($order_status == 35){
				$update_orderInfo_en['order_end_time'] = date("y-m-d H:i:s", time());
			}
			M('orderInfo', '', 'DB_trade')->where("order_id='%s'", $order_id)->save($update_orderInfo_en);
			
			
			//写入order_status_change_log begin
			$order_status_text	= StringBiz::getPromoterOrderStatusText($order_status);
			$order_status_change_log = array(
					'order_id'		=> $order_id,
					'order_status'	=> $order_status,
					'order_status_text'	=> $order_status_text,
					'remark'		=> '',
					'create_user'	=> 'whaleyFZ.admin stockOrderImportAction',
					'create_date'	=> date("y-m-d H:i:s", time()),
			);
			M('orderStatusChangeLog', '', 'DB_trade')->add($order_status_change_log);
			//写入order_status_change_log end
			//endregion A.更改order_info的订单状态
			
			//region B.更改promoter_order的订单状态
			$update_promoterOrder_en = array(
					'order_status' => $order_status
			);
			if($order_status == 35){
				$update_promoterOrder_en['order_end_date'] = date("y-m-d H:i:s", time());
			}
			M('promoterOrder')->where("order_id='%s'", $order_id)->save($update_promoterOrder_en);
            //endregion

		}

		//endregion 2.更改订单状态
	}
	
	
	
	
	
	
	
	public static function shipOrderExcel($params_en){
		Vendor('phpExcel.PHPExcel');
		$objPHPExcel = new  \PHPExcel();
		
		$objPHPExcel->setActiveSheetIndex(0);
		$objPHPExcel->getActiveSheet()->setTitle('导入');
		 
		//表头字母
		$header_letter_array = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 'AA', 'AB', 'AC');
		 
		//表头设置start
		$table_header_array = array(
				'订单类型(*)',
				'客户订单号(*)',
				'客户订单行号(*)',
				'前续客户订单号',
				'前续客户订单行号',
				'付款状态(*)',
				'代收金额(元)(*)',
				'收货人姓名(*)',
				'收货人移动电话(*)',
				'收货人固定电话',
				'收货人所在省(*)',
				'收货人所在市(*)',
				'收货人所在县/区(*)',
				'收货人详细地址(*)',
				'国标码',
				'预约送货时间',
				'预约到货时间',
				'客户物料号(*)',
				'订单数量(*)',
				'产品批次(*)',
				'发货仓库(*)',
				'收货仓库',
				'货主(*)',
				'服务方式(*)',
				'安装类型',
				'商品名称',
				'支付价格',
				'订单时间',
                '推广人姓名'
		);
		//表头设置end
		
		
		//设置表头(第一行)
		$row_index = 1;
		foreach($header_letter_array as $header_letter){
			$table_row_index = $row_index - 1;
			$objPHPExcel->getActiveSheet()->setCellValue($header_letter.strval(1),
					$table_header_array[$table_row_index]);
			$objPHPExcel->getActiveSheet()->getStyle($header_letter.strval(1))->getFont()->setBold(true);
		
			$row_index = $row_index + 1;
		}
		
		
		
		// 搜索条件start
 		$condition_array = array();
 		
		if(!empty($params_en['promoter_id'])){
			array_push($condition_array, "rso.promoter_id LIKE '%".$params_en['promoter_id']."%'");
		}
		
		if(!empty($params_en['promoter_name'])){
			array_push($condition_array, "rso.promoter_name LIKE '%".$params_en['promoter_name']."%'");
		}
		
		if(!empty($params_en['order_id'])){
			array_push($condition_array, "rso.order_id LIKE '%".$params_en['order_id']."%'");
		}
		
		if(!empty($params_en['begin_order_date'])){
			array_push($condition_array, "rso.order_date >= '".$params_en['begin_order_date']."'");
		}
		
		if(!empty($params_en['end_order_date'])){
			array_push($condition_array, "rso.order_date <= '".$params_en['end_order_date']."'");
		}
		
		if(!empty($params_en['order_status'])){
			array_push($condition_array, "o.order_status = ".$params_en['order_status']."");
		}
		
		if(!empty($params_en['payment_status'])){
			array_push($condition_array, "o.payment_status = ".$params_en['payment_status']."");
		}
		
		array_push($condition_array, "o.order_type = 5");
		// 搜索条件end

	
		//订单信息		
		$condition_sql = implode(' AND ', $condition_array);

		$sql = "SELECT rso.*, od.`style_name`, od.`payment_amount`, o.payment_status, o.seller_name 
                FROM rrs_stock_order rso
                    INNER JOIN order_detail od
                        ON rso.rrs_stock_order_id = od.`oid`
                    INNER JOIN order_info o
                        ON od.order_id = o.order_id
			WHERE 
		".$condition_sql." ORDER BY create_date DESC";

		$lst_rrsStockOrder_en = M('', '', 'DB_trade')
		->query($sql);
		
		
		//sku信息
		$lst_sku_en = M('sku', '', 'DB_trade')->where("is_buy_fz = 1")->select();
		
		
		//xls数据拼接start
		//行号，从第二行起
		$row_num = 2;
		foreach($lst_rrsStockOrder_en as $order_en){

			//订单类型
			$objPHPExcel->getActiveSheet()->setCellValue('A'.strval($row_num),
					'交易出库单');
			
			//客户订单号
			$objPHPExcel->getActiveSheet()->setCellValue('B'.strval($row_num),
					$order_en['rrs_stock_order_id']);
			
			//客户订单行号
			$objPHPExcel->getActiveSheet()->setCellValue('C'.strval($row_num),
					'1');
			
			//前续客户订单号
			$objPHPExcel->getActiveSheet()->setCellValue('D'.strval($row_num),
					'');
			
			//前续客户订单行号
			$objPHPExcel->getActiveSheet()->setCellValue('E'.strval($row_num),
					'');
				
			//付款状态(*)
			$objPHPExcel->getActiveSheet()->setCellValue('F'.strval($row_num),
					StringBiz::getRRSPaymentStatusText($order_en['payment_status']));
				
			//代收金额(元)(*)
			$objPHPExcel->getActiveSheet()->setCellValue('G'.strval($row_num),
					'0');
				
			//收货人姓名(*)
			$objPHPExcel->getActiveSheet()->setCellValue('H'.strval($row_num),
					$order_en['consignee_name']);
			
			//收货人移动电话(*)
			$objPHPExcel->getActiveSheet()->setCellValue('I'.strval($row_num),
					$order_en['consignee_cellphone']);
				
			//收货人固定电话(*)
			$objPHPExcel->getActiveSheet()->setCellValue('J'.strval($row_num),
					'');
			
			//收货人所在省市区
			$objPHPExcel->getActiveSheet()->setCellValue('K'.strval($row_num),
					$order_en['consignee_province']);
			
			$objPHPExcel->getActiveSheet()->setCellValue('L'.strval($row_num),
					$order_en['consignee_city']);
			
			$objPHPExcel->getActiveSheet()->setCellValue('M'.strval($row_num),
					$order_en['consignee_district']);
				
			//收货人详细地址(*)
			$objPHPExcel->getActiveSheet()->setCellValue('N'.strval($row_num),
					$order_en['consignee_address'].'【请签收前开箱验货】');
				
			//国标码
			$objPHPExcel->getActiveSheet()->setCellValue('O'.strval($row_num),
					'');
			
			//预约送货时间
			$objPHPExcel->getActiveSheet()->setCellValue('P'.strval($row_num),
					'');
			
			//预约到货时间
			$objPHPExcel->getActiveSheet()->setCellValue('Q'.strval($row_num),
					'');
			
			//客户物料号(*)-----
			$objPHPExcel->getActiveSheet()->setCellValue('R'.strval($row_num),
					$order_en['material_no']);
			
			//订单数量(*)
			$objPHPExcel->getActiveSheet()->setCellValue('S'.strval($row_num),
					$order_en['goods_qty']);
			
			//产品批次(*)
			$objPHPExcel->getActiveSheet()->setCellValue('T'.strval($row_num),
					'正品');
			
			//发货仓库(*)
			$objPHPExcel->getActiveSheet()->setCellValue('U'.strval($row_num),
					OrderBiz::getSendWarehouse($order_en['consignee_district_id']));
				
			//收货仓库
			$objPHPExcel->getActiveSheet()->setCellValue('V'.strval($row_num),
					'');
				
			//货主
			$objPHPExcel->getActiveSheet()->setCellValue('W'.strval($row_num),
					'o2o云推广');
			
			//服务方式
			$objPHPExcel->getActiveSheet()->setCellValue('X'.strval($row_num),
					'只配送不安装');
				
			//安装类型
			$objPHPExcel->getActiveSheet()->setCellValue('Y'.strval($row_num),
					'');
			
			//商品名称
			$objPHPExcel->getActiveSheet()->setCellValue('Z'.strval($row_num),
					$order_en['style_name']);
			
			//支付价格
			$objPHPExcel->getActiveSheet()->setCellValue('AA'.strval($row_num),
					$order_en['payment_amount']);
			
			//订单时间
			$objPHPExcel->getActiveSheet()->setCellValue('AB'.strval($row_num),
					$order_en['order_date']);

            //推广人姓名
            $objPHPExcel->getActiveSheet()->setCellValue('AC'.strval($row_num),
                $order_en['seller_name']);
							
			$row_num = $row_num + 1;
		}
		//xls数据拼接end
		
		return $objPHPExcel;
	}

	
	public static function DFYXshipOrderExcel($params_en){
		Vendor('phpExcel.PHPExcel');
		$objPHPExcel = new  \PHPExcel();
	
		$objPHPExcel->setActiveSheetIndex(0);
		$objPHPExcel->getActiveSheet()->setTitle('导入');
			
		//表头字母
		$header_letter_array = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y');
			
		//表头设置start
		$table_header_array = array(
				'订单类型(*)',
				'客户订单号(*)',
				'客户订单行号(*)',
				'前续客户订单号',
				'前续客户订单行号',
				'付款状态(*)',
				'代收金额(元)(*)',
				'收货人姓名(*)',
				'收货人移动电话(*)',
				'收货人固定电话',
				'收货人所在省(*)',
				'收货人所在市(*)',
				'收货人所在县/区(*)',
				'收货人详细地址(*)',
				'国标码',
				'预约送货时间',
				'预约到货时间',
				'客户物料号(*)',
				'订单数量(*)',
				'产品批次(*)',
				'发货仓库(*)',
				'收货仓库',
				'货主(*)',
				'服务方式(*)',
				'安装类型'
		);
		//表头设置end
	
	
		//设置表头(第一行)
		$row_index = 1;
		foreach($header_letter_array as $header_letter){
			$table_row_index = $row_index - 1;
			$objPHPExcel->getActiveSheet()->setCellValue($header_letter.strval(1),
					$table_header_array[$table_row_index]);
			$objPHPExcel->getActiveSheet()->getStyle($header_letter.strval(1))->getFont()->setBold(true);
	
			$row_index = $row_index + 1;
		}
	
	
	
		// 搜索条件start
		$condition_array = array();
			
		if(!empty($params_en['promoter_id'])){
			array_push($condition_array, "rso.promoter_id LIKE '%".$params_en['promoter_id']."%'");
		}
	
		if(!empty($params_en['promoter_name'])){
			array_push($condition_array, "rso.promoter_name LIKE '%".$params_en['promoter_name']."%'");
		}
	

	
		if(!empty($params_en['begin_order_date'])){
			array_push($condition_array, "rso.order_date >= '".$params_en['begin_order_date']."'");
		}
	
		if(!empty($params_en['end_order_date'])){
			array_push($condition_array, "rso.order_date <= '".$params_en['end_order_date']."'");
		}
	
		if(!empty($params_en['order_status'])){
			array_push($condition_array, "o.order_status = ".$params_en['order_status']."");
		}
	
		if(!empty($params_en['payment_status'])){
			array_push($condition_array, "o.payment_status = ".$params_en['payment_status']."");
		}
	
		array_push($condition_array, "o.order_type = 5");
		// 搜索条件end
	
	
		//订单信息
		$condition_sql = implode(' AND ', $condition_array);
	
		$sql = "
			SELECT rso.*, od.`style_name`, od.`payment_amount`, o.payment_status FROM rrs_stock_order rso
				INNER JOIN order_info o
					ON rso.order_id = o.order_id
				INNER JOIN order_detail od
					ON o.`order_id` = od.`order_id`
			WHERE
		".$condition_sql." ORDER BY create_date DESC";
	
		$lst_rrsStockOrder_en = M('', '', 'DB_trade')
		->query($sql);
	
	
		//sku信息
		$lst_sku_en = M('sku', '', 'DB_trade')->where("is_buy_fz = 1")->select();
	
	
		//xls数据拼接start
		//行号，从第二行起
		$row_num = 2;
		foreach($lst_rrsStockOrder_en as $order_en){
	
			//订单类型
			$objPHPExcel->getActiveSheet()->setCellValue('A'.strval($row_num),
					'交易出库单');
				
			//客户订单号
			$objPHPExcel->getActiveSheet()->setCellValue('B'.strval($row_num),
					$order_en['rrs_stock_order_id']);
				
			//客户订单行号
			$objPHPExcel->getActiveSheet()->setCellValue('C'.strval($row_num),
					'1');
				
			//前续客户订单号
			$objPHPExcel->getActiveSheet()->setCellValue('D'.strval($row_num),
					'');
				
			//前续客户订单行号
			$objPHPExcel->getActiveSheet()->setCellValue('E'.strval($row_num),
					'');
	
			//付款状态(*)
			$objPHPExcel->getActiveSheet()->setCellValue('F'.strval($row_num),
					StringBiz::getRRSPaymentStatusText($order_en['payment_status']));
	
			//代收金额(元)(*)
			$objPHPExcel->getActiveSheet()->setCellValue('G'.strval($row_num),
					'0');
	
			//收货人姓名(*)
			$objPHPExcel->getActiveSheet()->setCellValue('H'.strval($row_num),
					$order_en['consignee_name']);
				
			//收货人移动电话(*)
			$objPHPExcel->getActiveSheet()->setCellValue('I'.strval($row_num),
					$order_en['consignee_cellphone']);
	
			//收货人固定电话(*)
			$objPHPExcel->getActiveSheet()->setCellValue('J'.strval($row_num),
					'');
				
			//收货人所在省市区
			$objPHPExcel->getActiveSheet()->setCellValue('K'.strval($row_num),
					$order_en['consignee_province']);
				
			$objPHPExcel->getActiveSheet()->setCellValue('L'.strval($row_num),
					$order_en['consignee_city']);
				
			$objPHPExcel->getActiveSheet()->setCellValue('M'.strval($row_num),
					$order_en['consignee_district']);
	
			//收货人详细地址(*)
			$objPHPExcel->getActiveSheet()->setCellValue('N'.strval($row_num),
					$order_en['consignee_address'].'【请签收前开箱验货】');
	
			//国标码
			$objPHPExcel->getActiveSheet()->setCellValue('O'.strval($row_num),
					'');
				
			//预约送货时间
			$objPHPExcel->getActiveSheet()->setCellValue('P'.strval($row_num),
					'');
				
			//预约到货时间
			$objPHPExcel->getActiveSheet()->setCellValue('Q'.strval($row_num),
					'');
				
			//客户物料号(*)-----
			$objPHPExcel->getActiveSheet()->setCellValue('R'.strval($row_num),
					$order_en['material_no']);
				
			//订单数量(*)
			$objPHPExcel->getActiveSheet()->setCellValue('S'.strval($row_num),
					$order_en['goods_qty']);
				
			//产品批次(*)
			$objPHPExcel->getActiveSheet()->setCellValue('T'.strval($row_num),
					'正品');
				
			//发货仓库(*)
			$objPHPExcel->getActiveSheet()->setCellValue('U'.strval($row_num),
					OrderBiz::getSendWarehouse($order_en['consignee_district_id']));
	
			//收货仓库
			$objPHPExcel->getActiveSheet()->setCellValue('V'.strval($row_num),
					'');
	
			//货主
			$objPHPExcel->getActiveSheet()->setCellValue('W'.strval($row_num),
					'o2o云推广');
				
			//服务方式
			$objPHPExcel->getActiveSheet()->setCellValue('X'.strval($row_num),
					'只配送不安装');
	
			//安装类型
			$objPHPExcel->getActiveSheet()->setCellValue('Y'.strval($row_num),
					'');
				
			$row_num = $row_num + 1;
		}
		//xls数据拼接end
	
		return $objPHPExcel;
	}


    public static function DJPshipOrderExcel($params_en){
        Vendor('phpExcel.PHPExcel');
        $objPHPExcel = new \PHPExcel();

        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setTitle('导入');

        //表头字母
        $header_letter_array = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC');

        //表头设置start
        $table_header_array = array(
            '订单类型(*)',
            '客户订单号(*)',
            '客户订单行号(*)',
            '前续客户订单号',
            '前续客户订单行号',
            '付款状态(*)',
            '代收金额(元)(*)',
            '收货人姓名(*)',
            '收货人移动电话(*)',
            '收货人固定电话',
            '收货人所在省(*)',
            '收货人所在市(*)',
            '收货人所在县/区(*)',
            '收货人详细地址(*)',
            '国标码',
            '预约送货时间',
            '预约到货时间',
            '客户物料号(*)',
            '订单数量(*)',
            '产品批次(*)',
            '发货仓库(*)',
            '收货仓库',
            '货主(*)',
            '服务方式(*)',
            '安装类型',
            '商品名称',
            '支付价格',
            '订单时间',
            '推广人姓名'
        );
        //表头设置end


        //设置表头(第一行)
        $row_index = 1;
        foreach($header_letter_array as $header_letter){
            $table_row_index = $row_index - 1;
            $objPHPExcel->getActiveSheet()->setCellValue($header_letter.strval(1),
                $table_header_array[$table_row_index]);
            $objPHPExcel->getActiveSheet()->getStyle($header_letter.strval(1))->getFont()->setBold(true);

            $row_index = $row_index + 1;
        }



        // 搜索条件start
        $condition_array = array();

        if(!empty($params_en['promoter_id'])){
            array_push($condition_array, "rso.promoter_id LIKE '%".$params_en['promoter_id']."%'");
        }

        if(!empty($params_en['promoter_name'])){
            array_push($condition_array, "rso.promoter_name LIKE '%".$params_en['promoter_name']."%'");
        }



        if(!empty($params_en['begin_order_date'])){
            array_push($condition_array, "rso.order_date >= '".$params_en['begin_order_date']."'");
        }

        if(!empty($params_en['end_order_date'])){
            array_push($condition_array, "rso.order_date <= '".$params_en['end_order_date']."'");
        }

        if(!empty($params_en['order_status'])){
            array_push($condition_array, "o.order_status = ".$params_en['order_status']."");
        }

        if(!empty($params_en['payment_status'])){
            array_push($condition_array, "o.payment_status = ".$params_en['payment_status']."");
        }

        array_push($condition_array, "o.order_type = 13");
        // 搜索条件end


        //订单信息
        $condition_sql = implode(' AND ', $condition_array);

        $sql = "
			SELECT rso.*, od.`style_name`, od.`payment_amount`, o.payment_status, o.seller_name  FROM rrs_stock_order rso
				INNER JOIN order_info o
					ON rso.order_id = o.order_id
				INNER JOIN order_detail od
					ON o.`order_id` = od.`order_id`
			WHERE
		".$condition_sql." ORDER BY create_date DESC";

        $lst_rrsStockOrder_en = M('', '', 'DB_trade')
            ->query($sql);


        //sku信息
        $lst_sku_en = M('sku', '', 'DB_trade')->where("is_buy_fz = 1")->select();


        //xls数据拼接start
        //行号，从第二行起
        $row_num = 2;
        foreach($lst_rrsStockOrder_en as $order_en){

            //订单类型
            $objPHPExcel->getActiveSheet()->setCellValue('A'.strval($row_num),
                '交易出库单');

            //客户订单号
            $objPHPExcel->getActiveSheet()->setCellValue('B'.strval($row_num),
                $order_en['rrs_stock_order_id']);

            //客户订单行号
            $objPHPExcel->getActiveSheet()->setCellValue('C'.strval($row_num),
                '1');

            //前续客户订单号
            $objPHPExcel->getActiveSheet()->setCellValue('D'.strval($row_num),
                '');

            //前续客户订单行号
            $objPHPExcel->getActiveSheet()->setCellValue('E'.strval($row_num),
                '');

            //付款状态(*)
            $objPHPExcel->getActiveSheet()->setCellValue('F'.strval($row_num),
                StringBiz::getRRSPaymentStatusText($order_en['payment_status']));

            //代收金额(元)(*)
            $objPHPExcel->getActiveSheet()->setCellValue('G'.strval($row_num),
                '0');

            //收货人姓名(*)
            $objPHPExcel->getActiveSheet()->setCellValue('H'.strval($row_num),
                $order_en['consignee_name']);

            //收货人移动电话(*)
            $objPHPExcel->getActiveSheet()->setCellValue('I'.strval($row_num),
                $order_en['consignee_cellphone']);

            //收货人固定电话(*)
            $objPHPExcel->getActiveSheet()->setCellValue('J'.strval($row_num),
                '');

            //收货人所在省市区
            $objPHPExcel->getActiveSheet()->setCellValue('K'.strval($row_num),
                $order_en['consignee_province']);

            $objPHPExcel->getActiveSheet()->setCellValue('L'.strval($row_num),
                $order_en['consignee_city']);

            $objPHPExcel->getActiveSheet()->setCellValue('M'.strval($row_num),
                $order_en['consignee_district']);

            //收货人详细地址(*)
            $objPHPExcel->getActiveSheet()->setCellValue('N'.strval($row_num),
                $order_en['consignee_address'].'【请签收前开箱验货】');

            //国标码
            $objPHPExcel->getActiveSheet()->setCellValue('O'.strval($row_num),
                '');

            //预约送货时间
            $objPHPExcel->getActiveSheet()->setCellValue('P'.strval($row_num),
                '');

            //预约到货时间
            $objPHPExcel->getActiveSheet()->setCellValue('Q'.strval($row_num),
                '');

            //客户物料号(*)-----
            $objPHPExcel->getActiveSheet()->setCellValue('R'.strval($row_num),
                $order_en['material_no']);

            //订单数量(*)
            $objPHPExcel->getActiveSheet()->setCellValue('S'.strval($row_num),
                $order_en['goods_qty']);

            //产品批次(*)
            $objPHPExcel->getActiveSheet()->setCellValue('T'.strval($row_num),
                '正品');

            //发货仓库(*)
            $objPHPExcel->getActiveSheet()->setCellValue('U'.strval($row_num),
                OrderBiz::getSendWarehouse($order_en['consignee_district_id']));

            //收货仓库
            $objPHPExcel->getActiveSheet()->setCellValue('V'.strval($row_num),
                '');

            //货主
            $objPHPExcel->getActiveSheet()->setCellValue('W'.strval($row_num),
                'o2o云推广');

            //服务方式
            $objPHPExcel->getActiveSheet()->setCellValue('X'.strval($row_num),
                '只配送不安装');

            //安装类型
            $objPHPExcel->getActiveSheet()->setCellValue('Y'.strval($row_num),
                '');

            //商品名称
            $objPHPExcel->getActiveSheet()->setCellValue('Z'.strval($row_num),
                $order_en['style_name']);

            //支付价格
            $objPHPExcel->getActiveSheet()->setCellValue('AA'.strval($row_num),
                $order_en['payment_amount']);

            //订单时间
            $objPHPExcel->getActiveSheet()->setCellValue('AB'.strval($row_num),
                $order_en['order_date']);

            //推广人姓名
            $objPHPExcel->getActiveSheet()->setCellValue('AC'.strval($row_num),
                $order_en['seller_name']);

            $row_num = $row_num + 1;
        }
        //xls数据拼接end

        return $objPHPExcel;
    }


	
	public static function getSendWarehouse($district_id){
		$rrsShippingRegion_en = M('rrsShippingRegion', '', 'DB_trade')
		->where(array('recive_district_id'=>$district_id))
		->find();
		
		if(empty($rrsShippingRegion_en)){
			return '';
		}
		return $rrsShippingRegion_en['warehouse_name'];
	}
	
}